package com.spic.business.train.year.service.impl;

import com.spic.business.common.datascope.anotation.BusinessDataScope;
import com.spic.business.train.year.domain.TrainPlanYear;
import com.spic.business.train.year.domain.TrainPlanYearClassNumSubtable;
import com.spic.business.train.year.domain.TrainPlanYearLeaderNumberSubtable;
import com.spic.business.train.year.domain.TrainPlanYearSearchRecord;
import com.spic.business.train.year.mapper.TrainPlanYearMapper;
import com.spic.business.train.year.mapper.TrainPlanYearSearchRecordMapper;
import com.spic.business.train.year.service.ITrainPlanYearService;
import com.spic.common.core.text.UUID;
import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 年度培训计划Service业务层处理
 *
 * @author yangjie
 * @date 2021-11-24
 */
@Service
public class TrainPlanYearServiceImpl implements ITrainPlanYearService
{
    @Autowired
    private TrainPlanYearMapper trainPlanYearMapper;
    @Autowired
    private TrainPlanYearSearchRecordMapper trainPlanYearSearchRecordMapper;

    /**
     * 查询年度培训计划
     *
     * @param id 年度培训计划主键
     * @return 年度培训计划
     */
    @Override
    public TrainPlanYear selectTrainPlanYearById(String id)
    {
        return trainPlanYearMapper.selectTrainPlanYearById(id);
    }

    /**
     * 查询年度培训计划列表
     *
     * @param trainPlanYear 年度培训计划
     * @return 年度培训计划
     */
    @Override
    @BusinessDataScope(menuPath = "business/train/year/index")
    public List<TrainPlanYear> selectTrainPlanYearList(TrainPlanYear trainPlanYear)
    {
        return trainPlanYearMapper.selectTrainPlanYearList(trainPlanYear);
    }

    /**
     * 新增年度培训计划
     *
     * @param trainPlanYear 年度培训计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTrainPlanYear(TrainPlanYear trainPlanYear)
    {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        trainPlanYear.setId(uuid);
        trainPlanYear.setCreateTime(DateUtils.getNowDate());
        int rows = trainPlanYearMapper.insertTrainPlanYear(trainPlanYear);
        //新增页面的 培训机构 培训地点 培训对象
        insertTrainPlanSearch(trainPlanYear);
        insertTrainPlanClassNumSubtable(trainPlanYear);
        return rows;
    }


    /**
     * 修改年度培训计划
     *
     * @param trainPlanYear 年度培训计划
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTrainPlanYear(TrainPlanYear trainPlanYear)
    {
        trainPlanYear.setUpdateTime(DateUtils.getNowDate());
        trainPlanYearMapper.deleteTrainPlanClassNumSubtableByParentId(trainPlanYear.getId());
        trainPlanYearMapper.deleteTrainPlanYearLeaderNumberSubtableByParentId(trainPlanYear.getId());
        insertTrainPlanSearch(trainPlanYear);
        insertTrainPlanClassNumSubtable(trainPlanYear);
        return trainPlanYearMapper.updateTrainPlanYear(trainPlanYear);
    }

    /**
     * 批量删除年度培训计划
     *
     * @param ids 需要删除的年度培训计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTrainPlanYearByIds(String[] ids)
    {
        trainPlanYearMapper.deleteTrainPlanYearLeaderNumberSubtableByParentIds(ids);
        trainPlanYearMapper.deleteTrainPlanClassNumSubtableByParentIds(ids);
        return trainPlanYearMapper.deleteTrainPlanYearByIds(ids);
    }

    /**
     * 删除年度培训计划信息
     *
     * @param id 年度培训计划主键
     * @return 结果
     */
    @Override
    public int deleteTrainPlanYearById(String id)
    {
        trainPlanYearMapper.deleteTrainPlanYearLeaderNumberSubtableByParentId(id);
        trainPlanYearMapper.deleteTrainPlanClassNumSubtableByParentId(id);
        return trainPlanYearMapper.deleteTrainPlanYearById(id);
    }

    /**
     * 新增${subTable.functionName}信息
     *
     * @param trainPlanYear 年度培训计划对象
     */
    public void insertTrainPlanClassNumSubtable(TrainPlanYear trainPlanYear)
    {
        List<TrainPlanYearClassNumSubtable> trainPlanClassNumSubtableList = trainPlanYear.getTrainPlanClassNumSubtableList();
        List<TrainPlanYearLeaderNumberSubtable> trainPlanYearLeaderNumberSubtableList = trainPlanYear.getTrainPlanYearLeaderNumberSubtableList();
        String id = trainPlanYear.getId();
        if (StringUtils.isNotNull(trainPlanClassNumSubtableList))
        {
            List<TrainPlanYearClassNumSubtable> list = new ArrayList<TrainPlanYearClassNumSubtable>();
            for (TrainPlanYearClassNumSubtable trainPlanClassNumSubtable : trainPlanClassNumSubtableList)
            {
                String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                trainPlanClassNumSubtable.setId(uuid);
                trainPlanClassNumSubtable.setPlanYearId(id);
                list.add(trainPlanClassNumSubtable);
            }
            if (list.size() > 0)
            {
                trainPlanYearMapper.batchTrainPlanClassNumSubtable(list);
            }
        }

        if (StringUtils.isNotNull(trainPlanYearLeaderNumberSubtableList))
        {
            List<TrainPlanYearLeaderNumberSubtable> list = new ArrayList<TrainPlanYearLeaderNumberSubtable>();
            for (TrainPlanYearLeaderNumberSubtable trainPlanYearLeaderNumberSubtable : trainPlanYearLeaderNumberSubtableList)
            {
                String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                trainPlanYearLeaderNumberSubtable.setId(uuid);
                trainPlanYearLeaderNumberSubtable.setPlanYearId(id);
                list.add(trainPlanYearLeaderNumberSubtable);
            }
            if (list.size() > 0)
            {
                trainPlanYearMapper.batchTrainPlanYearLeaderNumberSubtable(list);
            }
        }
    }

    //新增页面的 培训机构 培训地点 培训对象
    private void insertTrainPlanSearch(TrainPlanYear trainPlanYear)
    {
        //  1==培训机构 2==培训地点 3==培训对象
        String trainPersoneel = trainPlanYear.getTrainPersoneel();//培训对象
        String trainMechanism = trainPlanYear.getTrainMechanism();//培训机构
        String trainPlace = trainPlanYear.getTrainPlace();        //培训地点
        //培训对象
        if(StringUtils.isNotEmpty(trainPersoneel)){
            if(trainPersoneel.indexOf(",")!=-1){
                String[] split = trainPersoneel.split(",");
                for (int i = 0; i < split.length; i++)
                {
                    TrainPlanYearSearchRecord searchRecord = trainPlanYearSearchRecordMapper.selectTrainPlanYearSearchRecordByName(split[i],trainPlanYear.getTrainDept());
                    if(searchRecord==null){
                        TrainPlanYearSearchRecord trainPlanYearSearchRecord = new TrainPlanYearSearchRecord();
                        trainPlanYearSearchRecord.setDeptId(trainPlanYear.getTrainDept());
                        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                        trainPlanYearSearchRecord.setId(uuid);
                        trainPlanYearSearchRecord.setType("3");
                        trainPlanYearSearchRecord.setContent(split[i]);
                        trainPlanYearSearchRecordMapper.insertTrainPlanYearSearchRecord(trainPlanYearSearchRecord);
                    }
                }
            }else{
                TrainPlanYearSearchRecord searchRecord = trainPlanYearSearchRecordMapper.selectTrainPlanYearSearchRecordByName(trainPersoneel,trainPlanYear.getTrainDept());
                if(searchRecord==null){
                    TrainPlanYearSearchRecord trainPlanYearSearchRecord = new TrainPlanYearSearchRecord();
                    String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                    trainPlanYearSearchRecord.setId(uuid);
                    trainPlanYearSearchRecord.setDeptId(trainPlanYear.getTrainDept());
                    trainPlanYearSearchRecord.setType("3");
                    trainPlanYearSearchRecord.setContent(trainPersoneel);
                    trainPlanYearSearchRecordMapper.insertTrainPlanYearSearchRecord(trainPlanYearSearchRecord);
                }
            }
        }
        // 培训机构
        if(StringUtils.isNotEmpty(trainMechanism)){
            TrainPlanYearSearchRecord searchRecord = trainPlanYearSearchRecordMapper.selectTrainPlanYearSearchRecordByName(trainMechanism,trainPlanYear.getTrainDept());
            if(searchRecord==null)
            {
                TrainPlanYearSearchRecord trainPlanYearSearchRecord = new TrainPlanYearSearchRecord();
                String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                trainPlanYearSearchRecord.setId(uuid);
                trainPlanYearSearchRecord.setDeptId(trainPlanYear.getTrainDept());
                trainPlanYearSearchRecord.setType("1");
                trainPlanYearSearchRecord.setContent(trainMechanism);
                trainPlanYearSearchRecordMapper.insertTrainPlanYearSearchRecord(trainPlanYearSearchRecord);
            }
        }
        //培训地点
        if(StringUtils.isNotEmpty(trainPlace)){
            TrainPlanYearSearchRecord searchRecord = trainPlanYearSearchRecordMapper.selectTrainPlanYearSearchRecordByName(trainPlace,trainPlanYear.getTrainDept());
            if(searchRecord==null)
            {
                TrainPlanYearSearchRecord trainPlanYearSearchRecord = new TrainPlanYearSearchRecord();
                String id = UUID.randomUUID().toString().replaceAll("-", "");
                trainPlanYearSearchRecord.setId(id);
                trainPlanYearSearchRecord.setDeptId(trainPlanYear.getTrainDept());
                trainPlanYearSearchRecord.setType("2");
                trainPlanYearSearchRecord.setContent(trainPlace);
                trainPlanYearSearchRecordMapper.insertTrainPlanYearSearchRecord(trainPlanYearSearchRecord);
            }
        }
    }
}
